package com.ict.txmsv2.controller.personInfo;

import com.ict.txmsv2.model.ClockInRecord;
import com.ict.txmsv2.model.MethodDescrition;
import com.ict.txmsv2.model.RespBean;
import com.ict.txmsv2.model.User;
import com.ict.txmsv2.services.SumtimeService;
import com.ict.txmsv2.services.UploadService;
import com.ict.txmsv2.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * @author linzzz
 * @desc ...
 * @date 2020-12-17
 */
@RestController
@RequestMapping("/info")
public class PersonInfoController {

    @Autowired
    UserService userService;

    @Autowired
    UploadService uploadService;

    @Autowired
    SumtimeService sumtimeService;

    @PostMapping("/encode")
    @MethodDescrition("修改密码")
    public RespBean updatePsw(Integer userId, String oldPsw, String newPsw){
        if (userService.updatePsw(userId,oldPsw,newPsw)){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("失败了");
    }

    @PutMapping("/")
    @MethodDescrition("修改个人信息")
    public RespBean updatePersonInfo(@RequestBody User user){
        if (userService.updateDetail(user) == 1){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("煜林的代码有问题");
    }

    @PostMapping("/")
    @MethodDescrition("上传头像")
    public RespBean uploadAvatar(Integer userId, MultipartFile file){
        if (uploadService.uploadAvatar(userId,file) == 1) {
            return RespBean.ok("上传成功");
        }
        return RespBean.error("煜林的代码有问题");
    }

    @MethodDescrition("/获取个人打卡记录")
    @GetMapping("/")
    public List<ClockInRecord> getPersonalCIR(Integer userId, String date) throws Exception {
        return sumtimeService.getPersonalCIR(userId,date);
    }
}
